package com.xh.one2one.lesson.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.model.Student;
import com.xh.bussiness.system.service.IStudentService;
import com.xh.bussiness.thirdapi.xinzhou.bean.request.XzGroupScheduleRequest;
import com.xh.bussiness.thirdapi.xinzhou.bean.request.XzScheduleRequest;
import com.xh.bussiness.thirdapi.xinzhou.bean.request.XzStudentInfoRequest;
import com.xh.bussiness.thirdapi.xinzhou.model.XzGroupSchedule;
import com.xh.bussiness.thirdapi.xinzhou.model.XzRemainContactHour;
import com.xh.bussiness.thirdapi.xinzhou.model.XzSchedule;
import com.xh.bussiness.thirdapi.xinzhou.service.IXinzhouTmsService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.DateUtils;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import com.xh.one2one.base.model.StudentInfo;
import com.xh.one2one.lesson.bean.dto.LessonInfoDto;
import com.xh.one2one.lesson.bean.dto.LessonSummaryDto;
import com.xh.one2one.lesson.bean.request.LessonInfoRequest;
import com.xh.one2one.lesson.bean.request.LessonPlanRequest;
import com.xh.one2one.lesson.bean.request.LessonTimesRequest;
import com.xh.one2one.lesson.dao.LessonInfoMapper;
import com.xh.one2one.lesson.dao.LessonPlanMapper;
import com.xh.one2one.lesson.model.*;
import com.xh.one2one.lesson.service.*;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * 备课计划
 */
@Service
public class LessonPlanServiceImpl extends BaseServiceImpl<LessonPlanMapper, LessonPlan> implements ILessonPlanService {

    @Autowired
    private IStudentService studentService;

    @Override
    public XhPage queryByPage(XhPage<LessonPlan> page, LessonPlanRequest lessonPlanRequest) {
        QueryWrapper<LessonPlan> qw = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(lessonPlanRequest.getTeacherName())){
            qw.like("teacher_name", lessonPlanRequest.getTeacherName());
        }
        if(StringUtils.isNotEmpty(lessonPlanRequest.getStudentName())){
            qw.like("student_name", lessonPlanRequest.getStudentName());
        }
        if(lessonPlanRequest.getStartDate() != null){
            qw.ge("lesson_time", DateUtils.getDateStart(lessonPlanRequest.getStartDate()));
        }
        if(lessonPlanRequest.getEndDate() != null){
            qw.le("lesson_time", DateUtils.getDateEnd(lessonPlanRequest.getEndDate()));
        }
        qw.orderByDesc("lesson_time");
        super.dao.selectPage(page, qw);
        return page;
    }

    @Override
    public List queryByList(LessonInfoRequest bean) {
        QueryWrapper<LessonPlan> qw = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(bean.getUserId())){
            qw.eq("teacher_id", bean.getUserId());
        }
        if(bean.getStartDate() != null){
            qw.ge("lesson_time", DateUtils.getDateStart(DateUtils.parseDate(bean.getStartDate(), "yyyy-MM-dd")));
        }
        if(bean.getEndDate() != null){
            qw.le("lesson_time", DateUtils.getDateEnd(DateUtils.parseDate(bean.getEndDate(), "yyyy-MM-dd")));
        }
        qw.orderByDesc("lesson_time");
        return super.queryByCondition(qw);
    }

    @Override
    public List<Student> queryStudents() {
        QueryWrapper<LessonPlan> qw = new QueryWrapper<>();
        qw.eq("teacher_id", UserUtils.getCurrentUserId());
        List<LessonPlan> list = super.queryByCondition(qw);
        List<String> ids = new ArrayList<>();
        List<Student> students = new ArrayList<>();
        for (LessonPlan lessonPlan : list) {
            String stuIdsStr = lessonPlan.getStudentIds();
            if(StringUtils.isNotEmpty(stuIdsStr)){
                String[] stuIds = stuIdsStr.split(",");
                for (String stuId : stuIds) {
                    Student student = studentService.getById(stuId);
                    if(student != null){
                        students.add(student);
                        ids.add(stuId);
                    }
                }
            }
        }
        return students;
    }
}
